use strict;
use warnings;
use IO::Socket::INET;

bless(my $self = {}, __PACKAGE__);

sub remove_hook
{
	# Dummy subroutine, will override later
}

sub hook
{
	no warnings;
	my $orig_func = \&{*IO::Socket::new};
	*IO::Socket::new = sub
	{
		my ($package, $filename, $line) = caller();
		print "\nPackage: $package File: $filename Line: $line\n";
		print "--------------------- At Here ---------------------";
		if (open(my $fp, "<", $filename))
		{
			my @content = (<$fp>)[($line - 5 > 0 ? $line - 5 : 0) .. ($line + 5)];
			print @content, "\n";
		}

		print "\n";
		&$orig_func(@_);
	};

	# if current package exists a subroutine named "remove_hook", override it, do nothing else.
	if ($self->can("remove_hook"))
	{
		*remove_hook = sub
		{
			print "\n--------------------- removing ---------------------\n";
			*IO::Socket::new = $orig_func;
			print "\n--------------------- removed ---------------------\n";
		};
	}
}

hook();
my $sock = IO::Socket::INET->new("www.google.cn:80");

sub DESTROY
{
	print "\n--------------------- remove hook -----------------\n";
	if ($self->can("remove_hook"))
	{
		remove_hook();
	}
}
